package freespiritsocial.io.user.ui.fragment.message

import android.content.DialogInterface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.scwang.smart.refresh.layout.util.SmartUtil
import freespiritsocial.io.user.R
import freespiritsocial.io.user.base.BaseFragment
import freespiritsocial.io.user.chat.ChannelManager
import freespiritsocial.io.user.databinding.FrgSendMessageDialogBinding
import freespiritsocial.io.user.decoration.SpaceItemDecoration
import freespiritsocial.io.user.ext.clearData
import freespiritsocial.io.user.ext.hideSoftKeyboard
import freespiritsocial.io.user.ext.init
import freespiritsocial.io.user.ui.adapter.PeopleAdapter
import freespiritsocial.io.user.viewmodel.GetMyFollowsViewModel
import freespiritsocial.io.user.viewmodel.SearchFriendViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.hgj.jetpackmvvm.base.fragment.BaseBottomSheetDialogFragment
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction


class SendMessageListDialogFragment: BaseBottomSheetDialogFragment() {
    private val getMyFollowsViewModel by activityViewModels<GetMyFollowsViewModel>()
    private val searchViewModel by viewModels<SearchFriendViewModel>()

    companion object {
        fun crate(parent: BaseFragment<*,*>) = SendMessageListDialogFragment().apply {
            this.parent = parent
        }
    }

    private lateinit var parent: BaseFragment<*,*>
    private val adapter by lazy(LazyThreadSafetyMode.NONE) {
        PeopleAdapter(hasBtn = false).apply {
            setOnItemClickListener { _,_,position ->
                val userInfo = data[position]
                ChannelManager.createChannel(userInfo.resourceId) { url,nickName ->
                    parent.nav().navigateAction(
                        R.id.chatFrag,
                        bundleOf(ChatFrag.EXTRA_GROUP_CHANNEL_URL to url,ChatFrag.EXTRA_GROUP_CHANNEL_NAME to nickName)
                    )
                    dismiss()
                }
            }
        }
    }

    private lateinit var mDatabind: FrgSendMessageDialogBinding
    override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View? {
        mDatabind = DataBindingUtil.inflate(inflater,R.layout.frg_send_message_dialog,container,false)
        mDatabind.lifecycleOwner = this
        return mDatabind.root
    }

    override fun onDismiss(dialog: DialogInterface) {
        hideSoftKeyboard(mDatabind.etSearch)
        super.onDismiss(dialog)
    }

    override fun onViewCreated(view: View,savedInstanceState: Bundle?) {
        super.onViewCreated(view,savedInstanceState)
        mDatabind.model = searchViewModel
        mDatabind.recycler.init(adapter,emptyImage = R.mipmap.empty_people,emptyText = "No friends have been found yet.")
            .addItemDecoration(SpaceItemDecoration(0,SmartUtil.dp2px(15f)))
        topOffset = SmartUtil.dp2px(50f)
        //获取关注列表
        lifecycleScope.launch {
            getMyFollowsViewModel.resultFollow.collect {
                adapter.dealData(it)
            }
        }
        lifecycleScope.launch {
            searchViewModel.searchResultNetWork.collect {
                adapter.addData(it)
            }
        }
        searchViewModel.searchText.observe(this) {
            if (searchViewModel.allData.isEmpty()) searchViewModel.allData.addAll(adapter.data)
            adapter.clearData()
            searchViewModel.search()
        }
    }

}